<template>
  <view v-if="goods.goods_name">
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
      <swiper-item class="goods-list" v-for="(item, i) in goods.pics" :key="i">
        <image class="img" :src="item.pics_big" @click="perview(i)"></image>
      </swiper-item>
    </swiper>
    <view class="goods-info-box">
      <!-- 价格-->
      <view class="goods-price" style="color: #C00000; font-size: 20px;">￥{{goods.goods_price | tofixed}}</view>
      <!-- body-->
      <view class="goods-info-body">
        <!-- name-->
        <view class="goods-info-name">{{goods.goods_name}}</view>
        <!-- 收藏-->
        <view class="favi">
          <uni-icons type="star" size="15"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费-->
      <view class="yf">运费：免运费</view>
    </view>
    <view class="info">
      <rich-text :nodes="goods.goods_introduce"></rich-text>
    </view>
    <view class="goods-nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
    </view>

  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters,
  } from 'vuex'

  export default {
    data() {
      return {
        goods: {},
        options: [{
          icon: 'shop',
          text: '店铺',
          info: 0,
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      }
    },
    computed: {
      ...mapState('my_cart', ['cart']),
      ...mapGetters('my_cart', ['total']),
    },
    onLoad(options) {
      console.log(options)
      this.getGoodDetails(options.goods_id);
    },
    filters: {
      tofixed(num) {
        return Number(num).toFixed(2)
      }
    },
    methods: {
      ...mapMutations('my_cart', ['addToCart']),
      async getGoodDetails(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(
          /webg/, 'jpg')
        this.goods = res.message
      },
      perview(i) {
        uni.previewImage({
          current: i,
          urls: this.goods.pics.map(x => x.pics_big),
        })
      },
      onClick(e) {
        console.log(e);
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      buttonClick(e) {
        if (e.content.text === '加入购物车') {
          //{ goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
          
          const goods = {
            goods_id : this.goods.goods_id,
            goods_name : this.goods.goods_name,
            goods_price : this.goods.goods_price,
            goods_count : 1,
            goods_small_logo : this.goods.goods_small_logo,
            goods_state : this.goods.goods_state
          }
          this.addToCart(goods)
        }
      }
    },
    watch: {
      total :{
        handler(newVal){
          const findResult = this.options.find( x => x.text === '购物车')
          if(findResult){
            findResult.info = newVal
          } 
        },
        immediate: true,
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    .goods-list {
      .img {
        height: 100%;
        width: 100%;
      }
    }
  }

  .goods-info-box {
    padding: 10px;
    padding-right: 0;

    .goods-info-body {
      display: flex;

      .goods-info-name {
        font-size: 13px;
        border-right: 1px solid #efefef;
      }

      .favi {
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: gray;
        font-size: 12px;
      }
    }

    .yf {
      margin: 10px 0;
      font-size: 12px;
      color: gray;
    }
  }

  .info {
    padding-bottom: 50px;
  }

  .goods-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>
